package com.qf.service.impl;

import com.qf.cache.CategoryCache;
import com.qf.enums.CheckType;
import com.qf.exceptions.MyBaseException;
import com.qf.exceptions.MyErrorCode;
import com.qf.mapper.SysDeviceMapper;
import com.qf.mapper.SysSceneMapper;
import com.qf.pojo.SysCategory;
import com.qf.pojo.SysDevice;
import com.qf.pojo.SysScene;
import com.qf.pojo.SysUserInfo;
import com.qf.service.SysDeviceService;
import com.qf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * created by 赵文韬 on 2022/3/24 11:32
 */
@Service
@Transactional
public class SysDeviceServiceImpl implements SysDeviceService {
    @Autowired
    private SysSceneMapper sysSceneMapper;
    @Autowired
    private CategoryCache categoryCache;
    private SysDeviceMapper sysDeviceMapper;
    @Autowired
    public void setSysDeviceMapper(SysDeviceMapper sysDeviceMapper) {
        this.sysDeviceMapper = sysDeviceMapper;
    }

    @Override
    public void addSysDevice(SysDevice sysDevice) {
        Assert.isTrue(!sysDevice.isEmpty(CheckType.ADD), ()->{
            throw new MyBaseException("数据为空", MyErrorCode.DATA_NULL);
        });
        SysUserInfo loginUser = SecurityUtils.getLoginUser();
        Assert.notNull(loginUser,()->{
            throw new MyBaseException("请先登录",MyErrorCode.NOT_LOGIN );
        } );
        //将设备的创建者id设置为登录用户的id
        sysDevice.setBindUserId(loginUser.getUId());
        //根据设备id查询是否已经被绑定了
        SysDevice byDeviceId = sysDeviceMapper.findByDeviceId(sysDevice.getDeviceId());
        Assert.isNull(byDeviceId,()->{
            throw new MyBaseException("设备已经被绑定了",MyErrorCode.DEVICE_ALLREADY_BIND );
        } );
        //判断设备的有没有自己的场景
        if (sysDevice.getSceneId()==null){
            sysDevice.setSceneId(-1L);
        }else if (sysDevice.getSceneId()!=-1){
            SysScene scene = sysSceneMapper.findById(sysDevice.getSceneId());
            Assert.isTrue(scene==null||scene.getCreateBy()==loginUser.getUId(), ()->{
                throw new MyBaseException("场景不存在",MyErrorCode.SCENE_NOT_EXIST );
            });
        }
        //检查缓存中分类是否存在
        SysCategory category = categoryCache.getDelete(sysDevice.getCategyId());
        Assert.notNull(category, ()->{
            throw new MyBaseException("分类不存在",MyErrorCode.CATEGORY_NOT_EXIST );
        });
        sysDevice.setBindTime(new Date());
        sysDeviceMapper.addDevice(sysDevice);
    }

    @Override
    public SysDevice findByDeviceId(String DeviceId) {
        //登录验证
        SysUserInfo loginUser = SecurityUtils.getLoginUser();
        Assert.notNull(loginUser, ()->{
            throw new MyBaseException("请先登录",MyErrorCode.NOT_LOGIN );
        });
        SysDevice byDeviceId = sysDeviceMapper.findByDeviceId(DeviceId);
        //判断该设备是不是和当前用户绑定的
        Assert.isTrue(byDeviceId==null||byDeviceId.getBindUserId()==loginUser.getUId(),()->{
            throw new MyBaseException("设备不存在",MyErrorCode.DEVICE_ALLREADY_BIND );
        } );
        return byDeviceId;
    }

    @Override
    public int updateDevice(SysDevice sysDevice) {

        return sysDeviceMapper.updateDevice(sysDevice);
    }
}
